<?php defined('SYSPATH') or die('no direct access allowed.');

	global $dane;
	$dane = array();

	$depth =array();
	
	$file = url::base()."io_dir/out.xml";
	
	$text ="";

	$response_count = 0;
	
	function startElement($parser, $name, $attrs) 
	{
		global $term;
		global $response;
		global $courses_counter;

		if($name == "COURSESCHOICEQUERYRESPONSE")
		{
				$response = array();
				$courses_counter = 0;
				$term = array();
		}
	}

	function endElement($parser, $name) 
	{
		global $dane;
		global $term;
		global $response;
		global $response_count;
		global $courses_counter;
		global $text;

		if($name == "COURSESCHOICEQUERYRESPONSE")
		{
			$dane["RESPONSE".$response_count] = $response;
			$response_count+=1;
		}		
		elseif ($name == "COURSE")
		{
				$term["COURSE".$courses_counter] = $text;
				$courses_counter += 1;
		}
		elseif ($name == "TERM")
		{
				$term[$name] = $text;
		}
		elseif ($name == "ECTS")
		{
				$term[$name] = $text;
				$response["TERM".$term["TERM"]] = $term;
				$term = array();
				$courses_counter = 0; 
		}
	}

	function contents($parser, $data) { 

		global $text;
	
	  $data = preg_replace("/^\s+/", "", $data); 
    $data = preg_replace("/\s+$/", "", $data); 

//		$data = str_replace(">","",$data);

    if (!($data == ""))  { 
				$text = $data; 
    }
	} 
	

	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "contents"); 
	
	if (!($fp = fopen($file, "r"))) {
	    die("could not open XML input");
	}

	while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
	}

	xml_parser_free($xml_parser);
//print kohana::debug($this->input->post());
	View::factory('monday1705/results')->bind('result',$dane)->render(TRUE);


	//print kohana::debug($dane);
	echo "<br/><br/>\n";
	echo 'done in {execution_time} seconds';
?>
